package com.waijiaojun.tpo.web.admin;

import com.waijiaojun.tpo.entity.Category;
import com.waijiaojun.tpo.entity.Question;
import com.waijiaojun.tpo.entity.QuestionCategory;
import com.waijiaojun.tpo.entity.Scenario;
import com.waijiaojun.tpo.rest.RestClientException;
import com.waijiaojun.tpo.rest.RestResponse;
import com.waijiaojun.tpo.rest.RestServerException;
import com.waijiaojun.tpo.service.CategoryService;
import com.waijiaojun.tpo.service.QuestionService;
import com.waijiaojun.tpo.service.ScenarioService;
import com.waijiaojun.tpo.service.UserCategoryService;
import com.waijiaojun.tpo.utils.Constants;
import com.waijiaojun.tpo.utils.JsonUtil;
import com.waijiaojun.tpo.vo.BootstrapTableDataResponse;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: xvitcoder
 * Date: 12/20/12
 * Time: 5:27 PM
 */
@Controller
@RequestMapping("/admin")
public class AdminQuestionController {

    private static Logger logger = LoggerFactory.getLogger(AdminQuestionController.class);
    @Autowired
    private ScenarioService scenarioService;

    @Autowired
    private QuestionService questionService;


    @RequestMapping("article/{articleId}/question")
    public String questionList(@PathVariable("articleId") Long articleId, Model model) {
        logger.info(Constants.prefix_in + "articleId=" + articleId);
        Scenario scenario = scenarioService.findById(articleId);

        List<QuestionCategory> questionCategoryList = questionService.findAllQuestionCategoryList();

        model.addAttribute("article", scenario);
        model.addAttribute("questionCategoryList", JsonUtil.toJson(questionCategoryList));
        return "admin/questions";
    }

    @RequestMapping("question/list/{articleId}")
    @ResponseBody
    public RestResponse questionFindByArticleId(@PathVariable Long articleId) {
        RestResponse restResponse = null;
        try {
            logger.info(Constants.prefix_in + "articleId=" + articleId);//打印入参

            List<Question> questionList = questionService.findByArticleId(articleId);
            restResponse = RestResponse.getSuccessResponse(questionList);
        } catch (RestClientException e) {
            logger.error(e.getMessage(), e);
            restResponse = RestResponse.getClientErrorResponse(e.getMessage());
        } catch (RestServerException e) {
            logger.error(e.getMessage(), e);
            restResponse = RestResponse.getServerErrorResponse(e.getMessage());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            restResponse = RestResponse.getServerErrorResponse(e.getMessage());
        }
        //打印出参
        logger.info(Constants.prefix_out + JsonUtil.toJson(restResponse));
        return restResponse;
    }

    @RequestMapping("question/save")
    @ResponseBody
    public RestResponse categoryDelete(Question question) {
        RestResponse restResponse = null;
        try {
            logger.info(Constants.prefix_in + JsonUtil.toJson(question));//打印入参

            questionService.save(question);
            restResponse = RestResponse.getSuccessResponse(question.getId());
        } catch (RestClientException e) {
            logger.error(e.getMessage(), e);
            restResponse = RestResponse.getClientErrorResponse(e.getMessage());
        } catch (RestServerException e) {
            logger.error(e.getMessage(), e);
            restResponse = RestResponse.getServerErrorResponse(e.getMessage());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            restResponse = RestResponse.getServerErrorResponse(e.getMessage());
        }
        //打印出参
        logger.info(Constants.prefix_out + JsonUtil.toJson(restResponse));
        return restResponse;
    }

    @RequestMapping("question/uploadAudio/{questionId}")
    @ResponseBody
    public RestResponse uploadAudio(@PathVariable("questionId") Long questionId, @RequestParam("file") MultipartFile file) {
        RestResponse restResponse = null;
        try {
            logger.info(Constants.prefix_in + "questionId=" + questionId + ", file:" + file.getOriginalFilename() + ", " + file.getSize() + ", " + file.getContentType());//打印入参

            String audioUrl = questionService.uploadAudio(questionId,file);
            restResponse = RestResponse.getSuccessResponse(audioUrl);
        } catch (RestClientException e) {
            logger.error(e.getMessage(), e);
            restResponse = RestResponse.getClientErrorResponse(e.getMessage());
        } catch (RestServerException e) {
            logger.error(e.getMessage(), e);
            restResponse = RestResponse.getServerErrorResponse(e.getMessage());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            restResponse = RestResponse.getServerErrorResponse(e.getMessage());
        }
        //打印出参
        logger.info(Constants.prefix_out + JsonUtil.toJson(restResponse));
        return restResponse;
    }

    @RequestMapping("question/delete/{id}")
    @ResponseBody
    public RestResponse questionDelete(@PathVariable Long id) {
        RestResponse restResponse = null;
        try {
            logger.info(Constants.prefix_in + "id=" + id);//打印入参
            questionService.delete(id);
            restResponse = RestResponse.getSuccessResponse();
        } catch (RestClientException e) {
            logger.error(e.getMessage(), e);
            restResponse = RestResponse.getClientErrorResponse(e.getMessage());
        } catch (RestServerException e) {
            logger.error(e.getMessage(), e);
            restResponse = RestResponse.getServerErrorResponse(e.getMessage());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            restResponse = RestResponse.getServerErrorResponse(e.getMessage());
        }
        //打印出参
        logger.info(Constants.prefix_out + JsonUtil.toJson(restResponse));
        return restResponse;
    }


}
